﻿using _2022_2_28.Areas.StudentArea.Models.Students;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace _2022_2_28.Areas.StudentArea.Controllers
{
    [Area("StudentArea")]
    public class WelecomeStudentController : Controller
    {
        private IStudentsBll _bll;
        public WelecomeStudentController(IStudentsBll bll)
        {
            _bll = bll;
        }
        public async Task<IActionResult> Index()
        {
            string a = HttpContext.Session.GetString("key");
            if (a != null)
            {
                var id = Guid.Parse(a);
                var data = await _bll.GetStudentsByIdAsync(id);
                ViewBag.Students = data;
                ViewBag.Title = "主页面";
                return View();
            }
            else 
            {
                return RedirectToAction("Index","LogIn");
            }
        }

        [HttpGet]
        public async Task<IActionResult> ChangePwd() 
        {
            string a = HttpContext.Session.GetString("key");
            if (a != null)
            {
                var id = Guid.Parse(a);
                var data = await _bll.GetStudentsByIdAsync(id);
                ViewBag.Students = data;
                ViewBag.Title = "修改密码";
                return View(new ChangePwdViewModel());
            }
            else
            {
                return RedirectToAction("Index", "LogIn");
            }
        }
        [HttpPost]
        public async Task<IActionResult> ChangePwd(ChangePwdViewModel model) 
        {
            string a = HttpContext.Session.GetString("key");
            Guid id = Guid.Parse(a);
            if (ModelState.IsValid) 
            {
                int res = await _bll.UpdatePwdAsync(id,model.Pwd,model.NewPwd);
                if (res > 0)
                    return Redirect("~/StudentArea/WelecomeStudent/Index");
            }
            var data = await _bll.GetStudentsByIdAsync(id);
            ViewBag.Students = data;
            ViewBag.Title = "修改密码";
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> SignOut() 
        {
            HttpContext.Session.Remove("key");
            return Json("{\"result\":\"Ok\"}");
        }

        [HttpGet]
        public async Task<IActionResult> UserMessage() 
        {
            string a = HttpContext.Session.GetString("key");
            if (a != null)
            {
                var id = Guid.Parse(a);
                var data = await _bll.GetStudentsByIdAsync(id);
                ViewBag.Students = data;
                ViewBag.Title = "个人信息";
                return View(new UserMessage
                {
                    Id = id,
                    Name = data.Name,
                    Year = data.Birthday.Year,
                    Month = data.Birthday.Month,
                    Date = data.Birthday.Day,
                    Gender = data.Gender,
                    Tel = data.Tel,
                    Email = data.Email,
                    Photo = data.Photo,
                    WeChat = data.WeChat,
                    QQNumber = data.QQNumber,
                    Address = data.Address,
                    GradesId = data.GradesId,
                });
            }
            else 
            {
                return RedirectToAction("Index","Login");
            }
        }

        public async Task<IActionResult> ValidateTel(string tel) 
        {
            string a = HttpContext.Session.GetString("key");
            Guid id = Guid.Parse(a);

            var res = await _bll.IsExistAsync(id ,tel);
            return Json(res);
        }

    }
}
